from flask import Flask, request, jsonify
import pandas as pd
from itertools import combinations

app = Flask(__name__)

frequent_itemsets = pd.read_pickle('./frequent_itemsets.pkl')
rules = pd.read_pickle('./rules.pkl')

@app.route('/recommend',methods=['POST'])
def recommend():
    data = request.json.get('item', [])


    recommendDations = []
    for idx, rules in rules.iterrow():
        antecedents = list(rule['antecedents'])
        consequents = list(rule['consequents'])

        if set(antecedents).issubset(set(data)):
            recommendDations.extend(consequents)

    recommendDations = list(set(recommendDations) - set(data))
    return jsonify({"recommendDations":recommendDations})

if __name__ == '__main__':
    app.run(debug=True)
